﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using SharedLib.Helpers;
using SharedLib.XmlDataModel;

namespace Tracy.DataModel
{
    public class Resource : XmlDataModelWithAttributesAndID
    {

        public Resource() : base() { Status = "New"; }
        public Resource(XmlNode node)
            : base(node)
        {
            if(String.IsNullOrEmpty(Status)) Status = "New";
        }

        public override string RootNodeName
        {
            get
            {
                return "resource";
            }
        }

        /// <summary>
        /// Title of the resource
        /// </summary>
        public string Title
        {
            get { return Properties["title"]; }
            set { Properties["title"] = value; }
        }

        /// <summary>
        /// Url of the resource
        /// </summary>
        public string Url
        {
            get { return Properties["url"]; }
            set { Properties["url"] = value; }
        }



        //public override string Name
        //{
        //    get
        //    {
        //        return Title;
        //    }
        //    set
        //    {
        //        Title = value;
        //    }
        //}

        public string Status
        {
            get { return Attributes["status"]; }
            set { Attributes["status"] = value; }
        }
        public string Created
        {
            get { return Attributes["created"]; }
            set { Attributes["created"] = value; }
        }
        private SortedSet<FanSubGroup> _fanSubGroups;

        public SortedSet<FanSubGroup> FanSubGroups
        {
            get { if (_fanSubGroups == null)_fanSubGroups = new SortedSet<FanSubGroup>(); return _fanSubGroups; }
            set { _fanSubGroups = value; }
        }

        private Format _format;

        public Format Format
        {
            get { return _format; }
            set
            {
                Properties["format"] = value.Name;
                _format = value;
            }
        }

        private Resolution _resolution;

        public Resolution Resolution
        {
            get { return _resolution; }
            set
            {
                Properties["resolution"] = value.Name;
                _resolution = value;
            }
        }
        private Entry _entry;

        public Entry Entry
        {
            get { return _entry; }
            set
            {
                if (value == null)
                {
                    Properties.Remove("entry-id");
                }
                else
                {
                    Properties["entry-id"] = value.ID;
                }
                _entry = value;
            }
        }

        private FeedSource _feedSource;

        public FeedSource FeedSource
        {
            get { return _feedSource; }
            set {
                if (value == null)
                {
                    Properties.Remove("feed-source");
                }
                else
                {
                    Properties["feed-source"] = value.Name;
                }
                _feedSource = value; }
        }


        protected override XmlElement GenerateXmlNode(XmlDocument doc)
        {
            XmlElement rootNode = base.GenerateXmlNode(doc);
            if (FanSubGroups.Count > 0)
            {
                XmlElement fanSubGroupsNode = doc.CreateElement("fan-sub-groups");
                foreach (FanSubGroup fanSubGroup in FanSubGroups)
                {
                    fanSubGroupsNode.AppendChild(XmlHelper.CreateElementWithTextValue("fan-sub-group", fanSubGroup.Name, doc));
                }
                rootNode.AppendChild(fanSubGroupsNode);
            }

            return rootNode;
        }

        protected override void ParseRootNode(XmlElement rootNode)
        {
            base.ParseRootNode(rootNode);
            foreach (string fanSubGroupName in XmlHelper.GetNodeValues(rootNode, "fan-sub-groups/fan-sub-group"))
            {
                FanSubGroups.Add(TracyFacade.Instance.FilterManager.FanSubGroupProvider.GetItemByName(fanSubGroupName));
            }

            string format = XmlHelper.GetNodeValue(rootNode, "format");
            if (!String.IsNullOrEmpty(format)) Format = TracyFacade.Instance.FilterManager.FormatProvider.GetItemByName(format);

            string resolution = XmlHelper.GetNodeValue(rootNode, "resolution");
            if (!String.IsNullOrEmpty(resolution)) Resolution = TracyFacade.Instance.FilterManager.ResolutionProvider.GetItemByName(resolution);

            string feedSource = XmlHelper.GetNodeValue(rootNode, "feed-source");
            if (!String.IsNullOrEmpty(feedSource)) FeedSource = TracyFacade.Instance.FilterManager.FeedSourceProvider.GetItemByName(feedSource);

            string entryID = XmlHelper.GetNodeValue(rootNode, "entry-id");
            if (!String.IsNullOrEmpty(entryID)) Entry = TracyFacade.Instance.EntryProvider.GetItemByID(entryID);
        }

        public string GetResourceType()
        {
            if (Url.EndsWith(".torrent")) return "BT";
            if (Url.StartsWith("ed2k")) return "Emule";
            return "Other";
        }

        public string GetFileName()
        {
            return Url.Substring(Url.LastIndexOf("/") + 1);
        }
    }
}
